package 博客Test;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        // 年龄给出无序
        Student s1 = new Student("兽宝一号",23);
        Student s2 = new Student("兽宝二号",10);
        Student s3 = new Student("兽宝三号",18);
        Student s4 = new Student("兽宝四号",35);

        // 定义Student类的数组,存储学生对象
        Student[] stuArray = {s1,s2,s3,s4};

        // 定义数组,存储年龄
        int[] arr = new int[4];

        for (int i = 0; i < stuArray.length; i++) {
            Student stu = stuArray[i];
            arr[i] = stu.age;
        }

        // 排序
        Arrays.sort(arr);

        // 根据年龄大小添加元素到集合stuArray中
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < stuArray.length; j++) {
                Student stu = stuArray[j];
                if(arr[i] == stu.age){
                    stuArray[i] = stu;
                }
            }
        }

        // 打印
        for (int i = 0; i < 4; i++) {
            Student stu = stuArray[i];
            System.out.println("name = "+stu.name+" age = "+stu.age);
        }





/*        int [] arr = {10,34,45,25,89,3,24};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));*/
    }
}
